package routers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"reggie/controller"
)

func TestRouter(group *gin.RouterGroup)  {

	// 该路由下使用了2个自定义中间件
	group.GET("/test", LocalMiddleWareA(), LocalMiddleWareB(), controller.Test01)
	//group.GET("/test", controller.Test01)

	group.Use(LocalMiddleWareB())

	group.GET("/test2", controller.Test02)

	group.GET("/test3", controller.Test03)

}

func LocalMiddleWareA() gin.HandlerFunc {
	return func(c *gin.Context) {
		fmt.Println("MiddlewareA before request")
		// before request

		c.Set("testCtxParam", "testCtxValue")
		c.Next()

		//c.Abort()
		// after request
		fmt.Println("MiddlewareA after request")
	}
}

func LocalMiddleWareB() gin.HandlerFunc {
	return func(c *gin.Context) {
		fmt.Println("LocalMiddleWareB before request")
		// before request

		get, exists := c.Get("testCtxParam")
		if !exists {
			fmt.Println("get fail")
			return
		}
		fmt.Println("get = ", get)
		c.Next()

		// after request
		fmt.Println("LocalMiddleWareB after request")
	}
}